<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* I use the controller class to debug
 * Author Zijian Wang
 */

class Friend extends CI_Controller {


	function request()  // true/false
	{
		$f_sender= $_POST['f_sender'];
		$f_receiver = $_POST['f_receiver'];
		$f_content = $_POST['f_content'];;
		$f_type = $_POST['f_type'];

		$this->load->model('friend_model');
		$data = $this->friend_model->friendship_request($f_sender,$f_receiver,$f_content,$f_type);
		
		print $data;
	}

	function decision()   // true/false
	{
		$f_sender= $_POST['f_sender'];
		$f_receiver = $_POST['f_receiver'];
		$f_response = $_POST['f_response'];

		$this->load->model('friend_model');
		$data = $this->friend_model->request_decision($f_sender,$f_receiver,$f_response);
		
		print $data;
	}


    function kill()   // true/false
	{
		$f_response = $_POST['f_response'];
		$time = $_POST['time'];

		$this->load->model('friend_model');
		$data = $this->friend_model->elimiante_requests($f_response, $time);
		
		print $data;
	}


	function terminate_friend()   // true/false
	{
		$f_sender = $_POST['f_sender'];
		$f_receiver = $_POST['receiver'];

		$this->load->model('friend_model');
		$data = $this->friend_model->terminate_friendship($f_sender, $f_receiver);
		
		print $data;
	}


	/**
	 * show a guy's friend's list
	 *  
	 */
    function lists()    // a list
	{
		$u_name;
		if(isset($_POST['u_name']))
			$u_name = $_POST['u_name'];
		else{
			$u_name = get_cookie('username');
		}
		$start = 0;
		$end = 100;
		if(isset($_POST['start']))
			$start= $_POST['start'];
		if(isset($_POST['end']))
			$end = $_POST['end'];

		$this->load->model('Friend_model');
		if($data['friends'] = $this->Friend_model->friend_list($u_name, $start, $end)){
			$output = $this->smarty->view('panel/all_friend_panel.tpl',$data,true);
			$this->output->set_output($output);
		}else{
			$output = $this->smarty->view('warn/warn.tpl',array("msg"=>"You dont have friend!"),true);
			$this->output->set_output($output);
		}
	}

	/**
	 * show all the unanswer requests of a user
	 * Enter description here ...
	 */
	function unanswered_req()   // a list
	{
		$user = get_cookie("username");
		$start = 0;
		$end = 100;
		if(isset($_POST['start']))
			$start= $_POST['start'];
		if(isset($_POST['end']))
			$end = $_POST['end'];
		
		$this->load->model('Friend_model');
		$request = $this->Friend_model->unanswered_request($user, $start, $end);
		if(count($request) == 0){
			$output = 	$this->smarty->view('panel/request_panel.tpl',
										array("empty"=>true),
										true);
			$this->output->set_output($output);
		}else{
			$output = $this->smarty->view('panel/request_panel.tpl',array('empty'=>false,'request'=>$request),true);
			$this->output->set_output($output);
		}
	}


	function show_fri_profile()   // a list
	{
		$u_name = $_POST['u_name'];
		$start= $_POST['start'];
		$end = $_POST['end'];;

		$this->load->model('friend_model');
		$data = $this->friend_model->show_friend_profile($user, $start, $end);
		
		var_dump($data);
	}


	
	function count_unanswer_req()   // a list
	{
		$u_name = $_POST['u_name'];;
		$this->load->model('friend_model');
		$data = $this->friend_model->unanswer_req_count($u_name);
		
		var_dump($data);
	}

	function subclass_list()    //array
	{
		$user =  $_POST['user'];
		
		$this->load->model('friend_model');
		$data = $this->friend_model->all_subclass($user);
		
		var_dump($data);
	}


	function subclass_friend()    // array
	{
		$user = $_POST['user'];
		$f_type = $_POST['f_type'];

		
		$this->load->model('friend_model');
		$data = $this->friend_model->all_subclass_friend($user,$f_type);
		
		var_dump($data);
	}

	
	function update_friend_type(){
		$receiver = $_POST['user'] ;
		$sender   = $_POST['sender'];
		$f_type   = 'train_buddies';
		if(isset($_POST['f_type'])){
			$f_type   = $_POST['f_type'];
		}
		$this->load->model('friend_model');
		$data = $this->friend_model->update_friendship($sender,$receiver,$f_type);
	}

}

?>
